// +----------------------------------------------------------------------
// | JavaWeb_EleVue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2021 上海JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <javaweb520@gmail.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package com.javaweb.admin.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaweb.admin.entity.KdlPurchasein;
import com.javaweb.admin.entity.KdlPurchaseinReturn;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javaweb.admin.entity.KdlPurchaseinReturnB;
import com.javaweb.admin.entity.PurchaseinInspectionResult;
import com.javaweb.admin.query.KdlPurchaseinQuery;
import com.javaweb.admin.query.KdlPurchaseinReturnQuery;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 采购退货 Mapper 接口
 * </p>
 *
 * @author 鲲鹏
 * @since 2024-01-26
 */
public interface KdlPurchaseinReturnMapper extends BaseMapper<KdlPurchaseinReturn> {

    IPage<KdlPurchaseinReturn> getPurchaseinReturnList(IPage<KdlPurchaseinReturn> page, @Param("req") KdlPurchaseinReturnQuery req);


    @Select("select isnull(convert(varchar(20),dateadd(mi,-5,max(sync_time)),120),'2024-03-28 0:00:00') from kdl_zst3_b")
    String GetMaxSyncTime();

    @Select("select t1.*,zst3.pgidate,zst3.pgiidname,t2.umrez,t2.materialgroup,t2.manufacturername scqy,t3.bindesc from kdl_zst3_b t1 inner join kdl_zst3 zst3 on t1.pid=zst3.id  left join kdl_material t2 on t1.materialcode=t2.materialcode left join kdl_storedoc t3 on t1.cahlocation=t3.bincode where pid=${pid} order by cahlineno")
    List<KdlPurchaseinReturnB> getPurchaseinReturnDetail(Integer pid);

    /*
    * 根据主键ID，库位代码获取回写NC接口的退货明细
    * */
    @Select("select t1.*,t2.stockstatus from kdl_zst3_b t1 left join kdl_storedoc t2 on t1.cahlocation=t2.bincode    where isnull(t1.status,'')<>'已回写' and pid=${pid} and cahlocation=#{storeLocation}  order by cahlineno")
    List<KdlPurchaseinReturnB> getPurchaseinReturnNCDetail(Integer pid,String storeLocation);


    /**
     *
     * @param pid 主键id
     * @return 返回明细中去重的库位列表
     */
    @Select("select distinct cahlocation from kdl_zst3_b where pid=${pid} and isnull(status,'')<>'已回写'")
    List<String> getStorageLocation(Integer pid);

    @Select("select distinct t1.id,vendorpo from kdl_zst3 t1 inner join kdl_zst3_b t2 on t1.id=t2.pid where t2.status='未回写' order by t1.id")
    List<KdlPurchaseinReturn> getPurchaseReturnSubmitList();

    @Select("select isnull(batch.vbatchcode,'')+','+isnull(batch.vdef12,'')+','+isnull(replace(batch.vdef16,'~',''),'') vbatchcode from ods_nc_po_order_b po_b left join ods_nc_scm_batchcode batch on po_b.pk_batchcode=batch.pk_batchcode where po_b.pk_order_b = '${vendorpolineno}'")
    String getSCMBatchcode(String vendorpolineno);

    @Select("select isnull(batch.vlotno,'') vlotno from ods_nc_po_order_b po_b left join ods_nc_med_lotno_148 batch on po_b.vfree6=batch.pk_lotno where po_b.pk_order_b = '${vendorpolineno}'")
    String getNCVlotno(String vendorpolineno);

}
